// components/zts-permission/zts-permission.js
const app = getApp();
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        // 有权限的authority数组
        permissionList: {
            type: Array,
            value: [],
        },
        // 内容的创建者
        createUid: {
            type: String,
            value: '',
        },
        // 特殊身份
        hasSpecialIdentity: {
            type: Boolean,
            value: false,
        },
    },

    /**
     * 组件的初始数据
     */
    data: {
        hasPermission: false,
    },

    lifetimes: {
        // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
        attached: function () {
            this.hasPermission();
        },
    },

    observers: {
        createUid: function () {
            this.hasPermission();
        },
        hasSpecialIdentity: function () {
            this.hasPermission();
        },
    },

    /**
     * 组件的方法列表
     */
    methods: {
        hasPermission() {
            let hasPermission =
                this.data.permissionList.includes(app.globalData.authority) ||
                app.globalData.uid === this.data.createUid ||
                this.data.hasSpecialIdentity;
            this.setData({
                hasPermission,
            });
        },
    },
});
